
/** 字符串开头判断 **/
String.prototype.startWith = function(s){
    if(s==null||s==""||this.length==0||s.length>this.length)
        return false;
    if(this.substr(0,s.length)==s)
        return true;
    else
        return false;
    return true;
}
/** 字符串结尾判断 **/
String.prototype.endWith = function(s){
    if(s==null||s==""||this.length==0||s.length>this.length)
        return false;
    if(this.substring(this.length-s.length)==s)
        return true;
    else
        return false;
    return true;
}

/** 获取 json 对象中的数据 **/
function getJsonValue(jsonObj, keyPath, defValue) {
    var tmpJson = jsonObj;

    if($.isArray(keyPath)){
        for(var i=0; i<keyPath.length; i++){
            var oneKey = keyPath[i];

            if(oneKey in tmpJson){
                tmpJson = tmpJson[oneKey];
            }else{
                tmpJson = null;
                break;
            }
        }
    }else{
        tmpJson = tmpJson[keyPath];
    }
    if(!tmpJson){
        tmpJson = defValue;
    }

    return tmpJson;
}

//日期格式化  var time2 = new Date().format("yyyy-MM-dd hh:mm:ss");
Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1,                 //月份
        "d+": this.getDate(),                    //日
        "h+": this.getHours(),                   //小时
        "m+": this.getMinutes(),                 //分
        "s+": this.getSeconds(),                 //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds()             //毫秒
    };
    //处理年份
    var reYear = /(y+)/;
    var resultYear = reYear.exec(fmt);
    if (resultYear)
    {
        var yearformatPart = resultYear[0];//匹配到的格式化字符
        var yearVal = (this.getFullYear() + "").substr(4 - yearformatPart.length);
        fmt = fmt.replace(yearformatPart, yearVal);
    }
    for (var k in o) {
        var re = new RegExp("(" + k + ")");
        var re = re.exec(fmt);
        if (re) {
            var Val = "" + o[k];//本次需要替换的数据
            var formatPart = re[0];//匹配到的格式化字符
            var replaceVal = (formatPart.length == 1) ? (Val) : (("00" + Val).substr(Val.length));
            fmt = fmt.replace(formatPart, replaceVal);
        }
    }
    return fmt;
}

/** 将 from 数据组成 json 对象 **/
function getFormData(formDom) {

    var unindexed_array = $(formDom).serializeArray();
    var formJson = {};

    $.map(unindexed_array, function (n, i) {
        formJson[n['name']] = n['value'];
    });

    return formJson;
}

/** 对象非空判断 **/
function isNotBlank(text) {
    var flag = false;

    if(text !== null && text !== undefined && text !== ''){
        flag = true;
    }

    return flag;
}

/** 对象非空判断 **/
function isBlank(text) {
    var flag = true;

    if(text !== null && text !== undefined && text !== ''){
        flag = false;
    }

    return flag;
}


function readAsDataURL(file){
    //检验是否为图像文件
    var files = file.files[0];
    if(!/image\/\w+/.test(files.type)){
        bootbox.alert("请正确选择图片");
        return false;
    }
    if(files.size>2*1024*1024){
        bootbox.alert("文件不能超过2M");
        return false;
    }
    var that = $(file);

    var reader = new FileReader();
    //将文件以Data URL形式读入页面
    reader.readAsDataURL(files);
    reader.onload=function(e){
        var height = 0;
        that.siblings().hide();
        that.parents('.uploadBox').css({
            background:"url("+this.result+")",
            backgroundSize:'cover'
        })
    }
}

/** ============================================== **/
/** json 是否为空 **/
function isJsonEmpty(object) {
    var isEmpty = true;

    if (object) {
        for (var name in object) {
            isEmpty = false;
            break;
        }
    }
    return isEmpty;
}

/** 清空json 的空key **/
function clearJsonObjectEmptyKey(jsonObj) {
    var jsonResult = {};

    for(var key in jsonObj){

        var value = jsonObj[key];

        if(isNotBlank(value)){
            jsonResult[key] = value;
        }
    }
    return jsonResult;
}

/** 将字符串转化为utf-8字节 */
function ToUTF8(str) {
    var result = new Array();
    var k = 0;
    for (var i = 0; i < str.length; i++) {
        var j = encodeURI(str[i]);
        if (j.length == 1) {
            // 未转换的字符
            result[k++] = j.charCodeAt(0);
        } else {
            // 转换成%XX形式的字符
            var bytes = j.split("%");
            for (var l = 1; l < bytes.length; l++) {
                result[k++] = parseInt("0x" + bytes[l]);
            }
        }
    }
    return result;
}

/** 将 byte 字节转化成十六进制 */
function Bytes2Str(arrBytes) {
    var str = ""
    for (var i = 0; i < arrBytes.length; i++) {
        var tmp;
        var num = arrBytes[i];
        if (num < 0) {
            //此处填坑，当byte因为符合位导致数值为负时候，需要对数据进行处理
            tmp = (255 + num + 1).toString(16);
        } else {
            tmp = num.toString(16);
        }
        if (tmp.length == 1) {
            tmp = "0" + tmp;
        }
        if (i > 0) {
            str += " " + tmp;
        } else {
            str += tmp;
        }
    }
    return str;
}

/** 将十六进制字符串转化为 byte 数组 */
function Str2Bytes(name) {
    var hexA = new Array();
    name.forEach(item => {
        if (item === 'ff') {
            return hexA
        } else {
            hexA.push(parseInt(item, 16))
        }
    })
    return hexA
}
/** UTF8 转化为字符串 */
function UTF8ToStr(arr) {
    let val = ''
    arr.forEach(item => {
        if (item < 127) {
            val += String.fromCharCode(item)
        } else {
            val += '%' + item.toString(16).toUpperCase()
        }
    })
    console.log(val)
    try {
        return decodeURI(val)
    } catch (err) {
        return val
    }
}